package com.zhijian.medical.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.zhijian.medical.dao.local.DigitalSliceCaseGroupMapper;
import com.zhijian.medical.entity.po.DigitalSliceCaseGroup;
import com.zhijian.medical.entity.vo.request.DigitalSliceCaseGroupSearchVo;
import com.zhijian.medical.entity.vo.response.DigitalSliceCaseGroupVo;
import com.zhijian.medical.service.DigitalSliceCaseGroupService;
import com.zhijian.medical.util.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 数字切片库-组业务处理层
 *
 * @author HCG
 * @version: 1.0
 * @date 2023-10-19 09:58:55
 */
@Slf4j
@Service
public class DigitalSliceCaseGroupServiceImpl implements DigitalSliceCaseGroupService {

    @Autowired
    private DigitalSliceCaseGroupMapper mapper;

    @Override
    public DigitalSliceCaseGroup selectById(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public DigitalSliceCaseGroupVo selectVoById(Long id) {
        return mapper.selectVoById(id);
    }

    @Override
    public void deleteById(Long id) {
        mapper.deleteByPrimaryKey(id);
    }

    @Override
    public Long insert(DigitalSliceCaseGroup digitalSliceCaseGroup) {
        Date now = new Date();
        digitalSliceCaseGroup.setCreateTime(now);
        digitalSliceCaseGroup.setUpdateTime(now);
        return mapper.insert(digitalSliceCaseGroup) > 0 ? digitalSliceCaseGroup.getId() : 0L;
    }

    @Override
    public boolean update(DigitalSliceCaseGroup digitalSliceCaseGroup) {
        Date now = new Date();
        digitalSliceCaseGroup.setUpdateTime(now);
        return mapper.updateByPrimaryKey(digitalSliceCaseGroup) != 0;
    }

    @Override
    public boolean updateByPrimaryKeySelective(DigitalSliceCaseGroup record) {
        Date now = new Date();
        record.setUpdateTime(now);
        return mapper.updateByPrimaryKeySelective(record) != 0;
    }

    @Override
    public Integer count(DigitalSliceCaseGroupSearchVo digitalSliceCaseGroupSearchVo) {
        return mapper.count(digitalSliceCaseGroupSearchVo);
    }

    @Override
    public List<DigitalSliceCaseGroupVo> selectList(DigitalSliceCaseGroupSearchVo digitalSliceCaseGroupSearchVo) {
        return mapper.selectList(digitalSliceCaseGroupSearchVo);
    }

    @Override
    public Page<DigitalSliceCaseGroupVo> selectPage(DigitalSliceCaseGroupSearchVo digitalSliceCaseGroupSearchVo) {
        //先直接查总数
        int count = count(digitalSliceCaseGroupSearchVo);
        //分页
        Page<DigitalSliceCaseGroupVo> page = new Page<>(true, count, digitalSliceCaseGroupSearchVo.getPageNum(), digitalSliceCaseGroupSearchVo.getPageSize());
        digitalSliceCaseGroupSearchVo.setStartIndex(page.getStartIndex());
        page.setPage(true);
        page.setTotalCount(count);
        List<DigitalSliceCaseGroupVo> responseVos = this.selectList(digitalSliceCaseGroupSearchVo);
        page.setData(responseVos);
        return page;
    }

    @Override
    public boolean insertBatch(List<DigitalSliceCaseGroup> entities) {
        return CollectionUtil.isNotEmpty(entities) && mapper.insertBatch(entities) > 0;
    }

}